import { ElMessage, ElMessageBox } from "element-plus"
import nProgress from "nprogress";
import { ElLoading } from 'element-plus'

// 消息提示
// type : 'success' | 'warning' | 'info' | 'error'
export function toast(
  message,
  type = "success",
  duration = 2000,
  dangerouslyUseHTMLString = false
) {
  ElMessage({
    message,
    type,
    duration,
    dangerouslyUseHTMLString,
  })
}

// 弹出确认、取消的消息框
// type : success，error，info,  warning
export function showModel(msg, type = "warning", title = "警告") {
  return ElMessageBox.confirm(msg, title, {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: type,
  })
}

// 弹出Prompt
export function showPrompt(tip, title='', inputValue) {
  return ElMessageBox.prompt(tip, title, {
    confirmButtonText: "确 认",
    cancelButtonText: "取 消",
    inputValue: inputValue || ''
  })
}

// 显示头部全屏Loading
export function showHeaderFullLoading() {
  nProgress.start()
}

// 隐藏头部全屏Loading
export function hideHeaderFullLoading() {
  nProgress.done()
}

// 打开全屏幕Loading
var FullScreenLoading = null
export function openFullScreenLoading () {
  FullScreenLoading = ElLoading.service({
    lock: true,
    text: 'Loading',
    background: 'rgba(0, 0, 0, 0.7)',
  })
}

// 关闭全屏幕Loading
export function closeFullScreenLoading (loading) {
  let myloading = loading || FullScreenLoading
  if(myloading) {
    myloading.close()
  }
}

// 将对象进行初始化
export function initializeObjectValues(obj) {
  const keys = Reflect.ownKeys(obj)

  keys.forEach(key => {
      const value = obj[key]
      let initialValue

      switch (typeof value) {
          case 'string':
              initialValue = ''
              break
          case 'number':
              initialValue = 0
              break
          case 'boolean':
              initialValue = false
              break
          case 'object':
              if (Array.isArray(value)) {
                  initialValue = []
              } else if (value === null) {
                  initialValue = null  // You can decide to set it to {} if preferred
              } else {
                  initialValue = {}  // Recursively initialize nested objects if needed
              }
              break
          default:
              initialValue = undefined  // For other types like function, symbol, etc.
      }

      obj[key] = initialValue
  })

  return obj
}


// 浅拷贝
export function shallowCopy(from, to) {
  // 使用 Reflect.ownKeys() 获取对象的所有属性键
  const keys = Reflect.ownKeys(from);

  // 循环遍历每个属性键
  for (let key of keys) {
      // 使用 Reflect.get() 获取原始对象上的属性值
      to[key] = from[key]
  }
  // 返回新对象
  return to
}

const myserver = 'http://localhost:7088'
export function toAbsoluteUrl(relativePath) {
  // 确保相对路径以斜杠开头
  const path = relativePath.startsWith('/') ? relativePath : `/${relativePath}`;
  // 返回完整的绝对路径
  return'http://localhost:7088' + path;
}


